﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Bon.It.Server.Interfaces;
using Bon_It.Model;

namespace Bon.It.Server
{
    public partial class ContractManager : IComoditiyGroup
    {
        #region Implementation of IComoditiyGroup

        public Warengruppe[] GetComodityGroups()
        {
            DataLoadOptions options = new DataLoadOptions();
            options.LoadWith<Warengruppe>(w => w.Artikel);
            using (var context = GetDataContext(options))
            {
                // get only the one that can be modified.
                return context.Warengruppe.Where(i=>i.CanBeModified).ToArray();
            }
        }

        public void StoreComoditiyGroup(Warengruppe item)
        {
            using (var context = GetDataContext())
            {
                if (item.Id.Equals(0))
                {
                    context.Warengruppe.InsertOnSubmit(item);
                    
                }else
                {
                    context.Warengruppe.Attach(item);
                }
                context.SubmitChanges();
            }
        }

        public void DeleteComodityGroup(Warengruppe item)
        {
            using (var context = GetDataContext())
            {
                if (item.Id.Equals(0))
                {
                    return;
                }
                context.Warengruppe.Attach(item);
                context.Warengruppe.DeleteOnSubmit(item);
                context.SubmitChanges();
            }
        }

        #endregion
    }
}
